﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CANO.Data.DAOs
{
	public class RolDAO
	{
		public int RolID { get; set; }
		public string Rol { get; set; }

		public RolDAO getRol(int id)
		{
			using (dcCanoDataContext context = new dcCanoDataContext())
			{
				var rol =
					from r in context.Rols
					where r.RolID == id
					select r;

				RolDAO retRol = new RolDAO();
				fillProperties(retRol, rol.SingleOrDefault());

				return retRol;
			}
		}

		private void fillProperties(RolDAO retRol, Rol rol)
		{
			retRol.RolID = rol.RolID;
			retRol.Rol = rol.Rol1;
		}

		public RolDAO GetRolByUserID(int id)
		{
			using (dcCanoDataContext context = new dcCanoDataContext())
			{
				var rol =
					from r in context.Rols
					join u in context.Usuarios on r.RolID equals u.Rol
					where u.UsuarioID == id
					select r;

				RolDAO retRol = new RolDAO();
				fillProperties(retRol, rol.SingleOrDefault());

				return retRol;
			}
		}

		public static List<RolDAO> getAll()
		{
			using (dcCanoDataContext context = new dcCanoDataContext())
			{
				List<RolDAO> listRol = new List<RolDAO>();

				var rolColl =
					from r in context.Rols
					select r;

				foreach (var rol in rolColl)
				{
					listRol.Add(new RolDAO {RolID = rol.RolID, Rol = rol.Rol1 });			
				}

				return listRol;
			}
		}
		}
}
